from django.contrib.auth.models import User
from andweb.django.models import models, BaseModel
from evaluate.models import Student, Teacher

class User(BaseModel):
    STU = 0
    TEA = 1
    ROLE = (
        (STU, '学生'),
        (TEA, '教师'),
    )

    BOY  = 1
    GIRL = 0
    SEX_CHOICE = (
        (BOY, "男生"),
        (GIRL, "女生"),
    )

    username = models.CharField("登录名", max_length=16)
    password = models.CharField("密码", max_length=40)
    email    = models.CharField("邮箱", max_length=32)
    nickname = models.CharField("姓名", max_length=16)
    phone    = models.CharField("电话", max_length=11, null=True)
    gender   = models.SmallIntegerField("性别", choices=SEX_CHOICE, default=BOY, null=True)
    article_count = models.IntegerField(default=0)
    follow_count  = models.IntegerField(default=0)
    fans_count    = models.IntegerField(default=0)
    role = models.SmallIntegerField("身份", choices=ROLE, null=True)

    def getname(self):
        return self.nickname or self.username

    def __str__(self):
        return self.nickname


class Article(BaseModel):

    # status
    DRAFT     = 0
    PUBLISHED = 1
    STATUS = (
        (DRAFT, "自己可见"),
        (PUBLISHED, "已发布"),
    )

    author        = models.ForeignKey(User, related_name='articles')
    title         = models.CharField("标题", max_length=255)
    tags          = models.CharField("标签", max_length=64)
    description   = models.CharField("摘要", max_length=512)
    content       = models.CharField("内容", max_length=4096)
    create_time   = models.DateTimeField("创建时间", auto_now_add=True)
    update_time   = models.DateTimeField("更新时间", auto_now=True)
    status        = models.SmallIntegerField("状态", choices=STATUS, default=DRAFT)
    view_count    = models.IntegerField(default=0)
    comment_count = models.IntegerField(default=0)
    # praise_count  = models.IntegerField(default=0)

    @property
    def tagv(self):
        return self.tags.split(',')

    @classmethod
    def find_public(cls):
        return cls.objects.filter(status=cls.PUBLISHED)


class ArticleTag(BaseModel):
    name = models.CharField(max_length=32, primary_key=True)


class ArticleComment(BaseModel):
    article = models.ForeignKey(Article)
    user    = models.ForeignKey(User)
    time    = models.DateTimeField("创建时间", auto_now_add=True)
    body    = models.CharField("评论内容", max_length=255)


# class Admin(BaseModel):
#   user_id = models.ForeignKey(User, primary_key=True)


class Follow(BaseModel):
    follower = models.OneToOneField(User, related_name='followers')
    target   = models.ForeignKey(User)